/**
 * 
 */
package neptune.manager.model.topology;

import java.util.ArrayList;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class NetworkInterface {

	public static final String PHYSICAL_INTERFACE = "physical";
	public static final String NAT = "NAT";
	public static final String HIF = "HIF";
	public static final String BRIDGE = "bridge";

	/**
	 * Il nome dato all'interfaccia dal sistema operativo
	 */
	private String interfaceName;
	/**
	 * Il nome del nodo che ospita questa interfaccia
	 */
	private String parentNode;
	/**
	 * Un codice testuale che indica come avviene il binding interfaccia
	 * virtuale-fisica
	 */
	private String bindingDescription;
	/**
	 * Gli indirizzi IP associati all'interfaccia
	 */
	private ArrayList<IPAddress> ipAddresses = new ArrayList<IPAddress>(0);
	/**
	 * Gli indirizzi MAC associati all'interfaccia
	 */
	private MACAddress macAddress;
	/**
	 * L'indirizzo di controllo definito su questa interfaccia, se presente
	 */
	private IPAddress controlAddress = null;
	/**
	 * Una stringa che identifica il tipo di interfaccia di rete
	 */
	private String type;

	/**
	 * @param interfaceName
	 * @param bindingDescription
	 * @param mac
	 */
	public NetworkInterface(String interfaceName, String bindingDescription,
			MACAddress mac) {
		this.interfaceName = interfaceName;
		this.bindingDescription = bindingDescription;

		ipAddresses = new ArrayList<IPAddress>(0);
		macAddress = mac;
	}

	public void addIpAddress(IPAddress ipAddress) {
		if (ipAddress.isControlAddress())
			controlAddress = ipAddress;

		this.ipAddresses.add(ipAddress);
	}

	/**
	 * Restituisce un indirizzo se presente all'interno di questa interfaccia o
	 * null in caso contrario.
	 */
	public Address getAddress(Address address) {
		return ipAddresses.get(ipAddresses.indexOf(address));
	}

	public String getBindingDescription() {
		return bindingDescription;
	}

	public IPAddress getControlAddress() {
		return controlAddress;
	}

	/**
	 * @return the interfaceName
	 */
	public String getInterfaceName() {
		return interfaceName;
	}

	public IPAddress[] getIpAddresses() {
		return (IPAddress[]) ipAddresses.toArray(new IPAddress[0]);
	}

	public MACAddress getMacAddress() {
		return macAddress;
	}

	public String getParentNode() {
		return parentNode;
	}

	public String getType() {
		return type;
	}

	/**
	 * Verifica se l'indirizzo dato � fra gli indirizzi(IP o MAC) di questa
	 * interfaccia
	 * 
	 * @param address
	 * @return
	 */
	public boolean hasAddress(Address address) {
		if (address instanceof IPAddress) {
			for (IPAddress ipAddress : ipAddresses) {
				if (ipAddress.equals(address))
					return true;
			}
		} else if (address instanceof MACAddress) {
			if (this.macAddress.equals(address))
				return true;
		}
		return false;
	}

	public boolean hasControlAddress() {
		if (controlAddress != null)
			return true;

		return false;
	}

	public void setBindingDescription(String bindingDescription) {
		this.bindingDescription = bindingDescription;
	}

	/**
	 * @param interfaceName
	 *            the interfaceName to set
	 */
	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	public void setMacAddress(MACAddress macAddress) {
		this.macAddress = macAddress;
	}

	public void setParentNode(String parentNode) {
		this.parentNode = parentNode;
	}

	public void setType(String type) {
		this.type = type;
	}
}
